package com.dq.domain.message.oneself.centre;

import com.alibaba.fastjson.JSONObject;
import com.dq.context.Constant;
import com.dq.vo.baseVo.VO;
import com.dq.domain.base.BaseEntity;
import com.dq.domain.message.oneself.ApprovalMessage;
import com.dq.domain.message.oneself.Message;
import com.dq.domain.orders.OrgOrder;
import com.dq.domain.org.Org;
import com.dq.domain.sys.user.user.BossUser;
import com.dq.domain.sys.user.user.OrgUser;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.service.ShiroService;
import com.dq.service.sys.user.BossUserService;
import com.dq.utils.ListUtils;
import com.dq.utils.SpringUtil;
import com.dq.utils.TimeUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.ManyToOne;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cf
 */
@Data
@Entity
@ApiModel("采购审批消息用户关联")
public class ApprovalUserCentre extends BaseEntity implements VO.ToVO {

    /**
     * 消息接收者
     */
    @ApiModelProperty("用户")
    @ManyToOne
    public SysUser caseUser;
    @ApiModelProperty("采购审批消息")
    @ManyToOne
    protected ApprovalMessage approvalMessage;

    @ApiModelProperty("消息状态")
    @Enumerated(EnumType.ORDINAL)
    @NotNull
    protected Message.MessageStatus messageStatus = Message.MessageStatus.noSee;


    public enum NextCheck{
        boss("boss审核"),
        finance("财务审批"),
        depot("仓库确认"),
        org("门店确认到货"),
        ;

        public String name;

        NextCheck(String name) {
            this.name = name;
        }
    }
    @Data
    @ApiModel("视图")
    public static class Vo implements VO {
        protected Long id;

        @ApiModelProperty("未读/已读")
        public String messageStatusEn;
        @ApiModelProperty("未读/已读")
        public String messageStatusCn;
        @ApiModelProperty("消息类型")
        public String messageTypeEn;
        @ApiModelProperty("消息类型")
        public String messageTypeCn;

        @ApiModelProperty("创建时间")
        public String createTime;

        @ApiModelProperty("采购单信息")
        public JSONObject orgOrderJson;


        @ApiModelProperty("是否显示审批按钮")
        public Boolean isShow;
        @ApiModelProperty("用于判断下一步审核")
        public String nextCheckEn;
        @ApiModelProperty("流程图")
        public List<JSONObject> flowList;
    }

    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.id = this.id;

        vo.messageStatusEn = this.messageStatus.toString();
        vo.messageStatusCn = this.messageStatus.name;
        vo.messageTypeEn = this.approvalMessage.getMessageType().toString();
        vo.messageTypeCn = this.approvalMessage.getMessageType().name;

        vo.createTime = fmt(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("sysDeptId", this.approvalMessage.getOrgOrder().getOrg() == null ? null : this.approvalMessage.getOrgOrder().getOrg().getId());
        jsonObject.put("sysDeptName", this.approvalMessage.getOrgOrder().getOrg() == null ? null : this.approvalMessage.getOrgOrder().getOrg().getSimpleName());
        jsonObject.put("ordersNo", this.approvalMessage.getOrgOrder().getOrdersNo());
        // 采购时间
        jsonObject.put("time", fmt(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS));
        jsonObject.put("orgOrderStatusEn", this.approvalMessage.getOrgOrder().getOrgOrderStatus().toString());
        jsonObject.put("orgOrderStatusCn", this.approvalMessage.getOrgOrder().getOrgOrderStatus().name);
        jsonObject.put("price", this.approvalMessage.getOrgOrder().getPrice());
        jsonObject.put("orderId", this.approvalMessage.getOrgOrder().getId());
        vo.orgOrderJson = jsonObject;

        Map<String, Object> mapVo = getIsShow(this.approvalMessage.getOrgOrder());
        vo.isShow = mapVo.get("isShow") == null ? null : (boolean) mapVo.get("isShow");
        vo.nextCheckEn = mapVo.get("nextCheckEn") == null ? null : mapVo.get("nextCheckEn").toString();

        vo.flowList = getFlowList(this.approvalMessage.getOrgOrder());

        return vo;
    }

    public static Map<String, Object> getIsShow(OrgOrder orgOrder) {
        Map<String, Object> vo = new HashMap<>(16);
        OrgOrder.OrgOrderStatus orgOrderStatus = orgOrder.getOrgOrderStatus();

        ShiroService shiroService = SpringUtil.getBean(ShiroService.class);
        SysUser loginUser = shiroService.getUser();

        // 是否显示【审批/到货确认】按钮
        boolean isShow = false;
        if(loginUser != null) {
            BossUserService bossUserService = SpringUtil.getBean(BossUserService.class);

            if(orgOrderStatus.name.equals(OrgOrder.OrgOrderStatus.submitPass.name)) {
                // 提交审核, -> boss审核
                if(loginUser instanceof BossUser) {
                    /// 获取 负责本门店的 所有boss
                    List<BossUser> bossByOrgList = bossUserService.getBossByOrg((Org)orgOrder.getOrg());
                    for (BossUser bossUser : bossByOrgList) {
                        if(loginUser.getId().toString().equals(bossUser.getId().toString())) {
                            isShow = true;
                            vo.put("nextCheckEn",  NextCheck.boss.toString());
                            break;
                        }
                    }
                }
            }else if(orgOrderStatus.name.equals(OrgOrder.OrgOrderStatus.bossPass.name)) {
                List<String> roleCodesList = bossUserService.getRoleCodesByUser(loginUser);
                // boss审批确认 -> 财务审批
                if(ListUtils.listIn(Constant.ROLE_CODE_LIST.get(2), roleCodesList) != -1) {
                    isShow = true;
                    vo.put("nextCheckEn",  NextCheck.finance.toString());
                }
            }else if(orgOrderStatus.name.equals(OrgOrder.OrgOrderStatus.financialPass.name)) {
                List<String> roleCodesList = bossUserService.getRoleCodesByUser(loginUser);
                // 财务审批 -> 仓库确认
                if(ListUtils.listIn(Constant.ROLE_CODE_LIST.get(1), roleCodesList) != -1) {
                    isShow = true;
                    vo.put("nextCheckEn",  NextCheck.depot.toString());
                }
            }else if(orgOrderStatus.name.equals(OrgOrder.OrgOrderStatus.warehousePass.name)) {
                // 仓库确认 -> 门店确认到货
                if(loginUser instanceof OrgUser) {
                    if(loginUser.getId().toString().equals(orgOrder.getStaffUser().getId().toString())) {
                        isShow = true;
                        vo.put("nextCheckEn",  NextCheck.org.toString());
                    }
                }
            }
        }
        // 是否显示审批按钮
        vo.put("isShow", isShow);

        return vo;
    }

    /***
     * 获取流程图
     * @param orgOrder
     * @return
     */
    public static List<JSONObject> getFlowList(OrgOrder orgOrder) {
        OrgOrder.OrgOrderStatus orgOrderStatus = orgOrder.getOrgOrderStatus();

        // 初始化流程图
        List<JSONObject> flowList = ListUtils.copyList(ListUtils.copyList(Constant.FLOW_LIST));
        if(orgOrderStatus.name.equals(OrgOrder.OrgOrderStatus.submitPass.name)) {
            // 提交审核, -> boss审核
            flowList.get(0).put("isExecute", true);
        }else if(orgOrderStatus.name.equals(OrgOrder.OrgOrderStatus.bossPass.name) || orgOrderStatus.name.equals(OrgOrder.OrgOrderStatus.bossFefuse.name)) {
            // boss审批确认 -> 财务审批
            flowList.get(0).put("isExecute", true);
            flowList.get(1).put("isExecute", true);
        }else if(orgOrderStatus.name.equals(OrgOrder.OrgOrderStatus.financialPass.name) || orgOrderStatus.name.equals(OrgOrder.OrgOrderStatus.financialRefuse.name)) {
            // 财务审批 -> 仓库确认
            flowList.get(0).put("isExecute", true);
            flowList.get(1).put("isExecute", true);
            flowList.get(2).put("isExecute", true);
        }else if(orgOrderStatus.name.equals(OrgOrder.OrgOrderStatus.warehousePass.name) || orgOrderStatus.name.equals(OrgOrder.OrgOrderStatus.warehouseRefuse.name)) {
            // 仓库确认 -> 门店确认到货
            flowList.get(0).put("isExecute", true);
            flowList.get(1).put("isExecute", true);
            flowList.get(2).put("isExecute", true);
            flowList.get(3).put("isExecute", true);
        }else if(orgOrderStatus.name.equals(OrgOrder.OrgOrderStatus.orgPass.name) || orgOrderStatus.name.equals(OrgOrder.OrgOrderStatus.orgRefuse.name)) {
            // 门店确认到货 ->
            for (JSONObject jsonObject : flowList) {
                jsonObject.put("isExecute", true);
            }
        }

        return flowList;
    }

}
